<template>
	<view class="main">
		<v-tabs :scroll="false" activeColor="#FF6543" lineColor="#FF6543" v-model="current" :tabs="tabs"
			@change="changeTab"></v-tabs>
		<view v-if="current==0" class="lists">
			<view class="img-div" v-for="(item,index) in list" :key="index">
				<image :src="item.url" @click="toPre(item.url)"></image>
				<view class="btn" @click="toDownImg(item.url,index)">下载</view>
			</view>
		</view>
		<view v-if="current==1" class="lists">
			<view v-for="(item,index) in list" class="list">
				<view class="video-div">
					<video :src="item.url" controls></video>
					<view class="btn" @click="onStartDownload(item.url,index)">下载</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
	</view>
</template>

<script>
	import {
		materialList
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				current: 0,
				tabs: ['图片素材', '视频素材'],
				type: 1,
				group_id: '',
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				imgObjArr: []
			};
		},
		onLoad(option) {
			this.group_id = option.id
			this.getlist()
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			toDownImg(url, index) {
				console.log(index)
				uni.showLoading({
					title: '下载中...',
				})
				let that = this
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '下载成功'
									})
									that.list.splice(index, 1)
								},
								fail: function(err) {
									uni.showToast({
										title: '下载失败',
										icon: 'error'
									})
									uni.hideLoading();

								}

							})
						}
					},
					fail() {
						uni.showToast({
							title: '下载失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})

			},
			onStartDownloadImg(url) {
				wx.getSetting({
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.toDownImg(url)
								},
								fail: (err) => {
									console.log('授权失败:', err)
								}
							})
						} else if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.openSetting({
								success: (res) => {
									if (res.authSetting['scope.writePhotosAlbum']) {
										this.toDownImg(url)
									}
								}
							})
						} else {
							this.toDownImg(url)
						}
					}
				})
			},
			onStartDownload(url) {
				wx.getSetting({
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.downVideo(url)
								},
								fail: (err) => {
									console.log('授权失败:', err)
								}
							})
						} else if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.openSetting({
								success: (res) => {
									if (res.authSetting['scope.writePhotosAlbum']) {
										this.downVideo(url)
									}
								}
							})
						} else {
							this.downVideo(url)
						}
					}
				})
			},
			downVideo(url, index) {
				let that = this
				uni.showLoading({
					title: '下载中...',
				})
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							console.log(res.tempFilePath);
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '下载成功'
									})
									that.list.splice(index, 1)
								},
								fail() {
									uni.showToast({
										title: '下载失败',
										icon: 'error'
									})
									uni.hideLoading();
								}
							})
						}
					},
					fail() {
						uni.showToast({
							title: '下载失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})

			},
			toPre(i) {
				uni.previewImage({
					urls: [i],
					current: 0
				})
			},
			changeTab(e) {
				console.log(e)
				this.type = e + 1
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type
				} = that;
				let obj = {
					size: offset,
					page: page + 1,
					group_id,
					type
				};
				that.status = 'loading';
				if (totalPage > page) {
					let result = await materialList(obj);
					if (result.code == 1) {
						that.page = page + 1;
						if (result.data.total == 0) {
							console.log((888))
							that.empty = true;
							that.status = 'nomore';
							return;
						}
						that.empty = false;
						setTimeout(() => {
							that.status = 'loadmore';
						}, 500);
						that.list = list.concat(result.data.list);
						that.totalPage = Math.ceil(result.data.total / offset);
					}
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.lists {
		margin: 20rpx;
		display: grid;
		grid-row-gap: 20rpx;
		grid-column-gap: 20rpx;
		grid-template-columns: 1fr 1fr 1fr;
	}

	.img-div {
		// width: 25%;
		text-align: center;
		margin-bottom: 20rpx;
		background: white;
		padding: 20rpx;
		border-radius: 10rpx;

		image {
			width: 92%;
			height: 200rpx;
			object-fit: cover;
			background: white;
		}

		.btn {
			background: #FF6543;
			width: 120rpx;
			text-align: center;
			line-height: 48rpx;
			border-radius: 12rpx;
			color: white;
			margin: 10rpx auto 0;
			font-size: 26rpx;
		}
	}

	.list {
		background: white;
		border-radius: 12rpx;
		padding: 20rpx;

		image {
			width: 150rpx;
			height: 150rpx;
		}



		.list-info {
			margin-left: 20rpx;
			width: 73%;

			.name {
				font-size: 32rpx;
				font-weight: bold;
			}

			.time {
				color: gray;
				font-size: 28rpx;
				margin-top: 20rpx;
			}
		}


		.btn {
			background: #FF6543;
			width: 123rpx;
			text-align: center;
			line-height: 51rpx;
			border-radius: 12rpx;
			color: white;
			font-size: 28rpx;
		}

		.video-div {
			width: 100%;

			video {
				width: 100%;
				height: 200rpx;
				object-fit: fill;
			}

			.name {
				text-align: center;
				margin-bottom: 20rpx;
				font-weight: bold;
			}

			.btn {
				margin: 10rpx auto 0;
			}
		}
	}

	.main-btn {
		font-size: 34rpx;
		font-weight: bold;
		color: white;
		text-align: center;
		background: #FF6543;
		line-height: 108rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>